﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.API.Home.V1
{
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using LdCms.EF.DbModels;
    using LdCms.IBLL.Extend;
    using LdCms.IBLL.Info;
    using LdCms.Web.Services;


    /// <summary>
    /// 
    /// </summary>
    [ApiVersion("1.0")]
    [ControllerName("home")]
    public class HomeController : BaseApiController
    {
        private readonly IBaseApiManager BaseApiManager;
        private readonly IBlockService BlockService;
        private readonly IArticeService ArticeService;
        private readonly IAdvertisementDetailsService AdvertisementDetailsService;
        public HomeController(IBaseApiManager BaseApiManager, IBlockService BlockService, IArticeService ArticeService, IAdvertisementDetailsService AdvertisementDetailsService) : base(BaseApiManager)
        {
            this.BaseApiManager = BaseApiManager;
            this.BlockService = BlockService;
            this.ArticeService = ArticeService;
            this.AdvertisementDetailsService = AdvertisementDetailsService;

        }
        [HttpGet]
        [ActionName("index")]
        public IActionResult Index(string uuid)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(uuid);
                var adLists = GetAdvertisement();
                var banner = ToAdvertisementDetailsTDO(adLists.Where(x => x.AdvertisementID == "5019656100").ToList());
                string aboutContent = GetBook("4220566243");
                var aboutImages= ToAdvertisementDetailsTDO(adLists.Where(x => x.AdvertisementID == "5070200408").ToList());
                var homeAdvertisement= ToAdvertisementDetailsTDO(adLists.Where(x => x.AdvertisementID == "5079720691").ToList());
                var data = new
                {
                    banner = banner,
                    about = new
                    {
                        content =Utility.ContentDecode(aboutContent),
                        images = aboutImages.FirstOrDefault().GetObjectValues("media_path"),
                    },
                    ad = homeAdvertisement.FirstOrDefault(),
                    success_case = GetInfo("4312349932", 8),
                    partner = GetInfo("4393745145", 10),
                    elite_team = GetInfo("4319910968", 4),
                    news = GetInfo("4350667787", 3),
                };
                return Success(logId, "ok", data);
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }
        private IEnumerable<object> ToAdvertisementDetailsTDO(List<Ld_Extend_AdvertisementDetails> lists)
        {
            var data = from m in lists
                       select new
                       {
                           id = m.DetailsID.IIF(),
                           title = m.Title.IIF(),
                           remark = m.Remark.IIF(),
                           media_path = m.MediaPath.IIF(),
                           url = m.Url.IIF(),
                           sort = m.Sort.ToInt()
                       };
            return data;
        }
        private List<Ld_Extend_AdvertisementDetails> GetAdvertisement()
        {
            try
            {
                string[] advertisementId = new string[] { "5019656100", "5070200408", "5079720691" };
                string companyId = InterfaceAccount.CompanyID;
                return AdvertisementDetailsService.GetAdvertisementDetailsByAdvertisementId(SystemID, companyId, advertisementId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private string GetBook(string id)
        {
            try
            {
                string companyId = InterfaceAccount.CompanyID;
                var entity = BlockService.GetBlock(SystemID, companyId, id);
                if (entity == null)
                    return string.Empty;
                else
                    return entity.Content.IIF();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private object GetInfo(string id,int count)
        {
            try
            {
                string companyId = InterfaceAccount.CompanyID;
                string classId = id;
                var lists = ArticeService.GetArticeTop(SystemID, companyId, classId, "true", false, count);
                var data = from m in lists
                           select new
                           {
                               id = m.ArticeID.IIF(),
                               title = m.Title.IIF(),
                               title_brief = m.TitleBrief.IIF(),
                               images = m.ImgSrc.IIF(),
                               description = m.Description.IIF(),
                               date = m.CreateDate.ToDate().ToString("yyyy-MM-dd HH:mm:ss")
                           };
                return data;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



    }
}